package com.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionLikeType;

import java.util.ArrayList;
import java.util.List;

public class JsonUtil {
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    public static <T> List<T> json2List(String json, Class<T> tClass) {

        CollectionLikeType arrayType = OBJECT_MAPPER.getTypeFactory().constructCollectionLikeType(ArrayList.class, tClass);
        try {
            return OBJECT_MAPPER.readValue(json, arrayType);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String obj2Json(Object object) {
        try {
            return OBJECT_MAPPER.writeValueAsString(object);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
